﻿package com.thorqq.magictimer.timeLoop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.thorqq.magictimer.AdapterChooseMonthWeekDay;
import com.thorqq.magictimer.R;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TLoopPolicy;
import com.thorqq.magictimer.util.Util;
import com.thorqq.magictimer.widget.GridViewWithDivider;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

public class ChildViewLoopWeek extends ChildView
{
    @SuppressWarnings("unused")
    private ChildViewParent mParent;
    private TLoopPolicyWeek mWeekLoopPolicy;
    private Context mContext;
    private LayoutInflater mInflater;
    
    private ArrayList<Map<String, Object>> mWeekList = new ArrayList<Map<String, Object>>();
    private AdapterChooseMonthWeekDay mWeekAdapter;
    private GridViewWithDivider mWeekGridView;

    public ChildViewLoopWeek(Context context, TLoopPolicy policy)
    {
        mContext = context;
        mWeekLoopPolicy = (TLoopPolicyWeek) policy;
    }

    @Override
    public void updateLayout()
    {
        char[] arr = mWeekLoopPolicy.getDayMask();
        if(arr.length != TLoopPolicyWeek.DAYS_OF_WEEK)
        {
            return;
        }
            
    }

    @Override
    protected void registerListener()
    {

    }

    @Override
    protected void doInitLayout(ChildViewParent parent)
    {
        Util.log("init ChildViewLoopPolicyWeek");
        mInflater = LayoutInflater.from(mContext);
        mView = mInflater.inflate(R.layout.loop_item_child_week, null);
        mParent = parent;

        mWeekGridView = ((GridViewWithDivider) mView.findViewById(R.id.GridViewWithDividerWeek));
        mWeekAdapter = new AdapterChooseMonthWeekDay(mContext, mWeekList);
        mWeekGridView.setAdapter(mWeekAdapter);
        mWeekGridView.setHorizontalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_hor));
        mWeekGridView.setHorizontalDividerHeight(1);
        mWeekGridView.setVerticalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_ver));
        mWeekGridView.setVerticalDividerWidth(1);
        mWeekGridView.setColumnNum(7);
        
        // 循环策略
        mWeekList.clear();
        String[] weekText = {"日","一","二","三","四","五","六"};
        for(int i = 0; i < weekText.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text", weekText[i]);
            map.put("mask", mWeekLoopPolicy.getDayMask());
            map.put("listener", new WeekGridListener(mContext, i));
            mWeekList.add(map);
        }

        mWeekAdapter.notifyDataSetChanged();   
    }
    
    class WeekGridListener implements OnClickListener 
    {
        private int mPos;
        @SuppressWarnings("unused")
        private Context mContext;

        WeekGridListener(Context context, int pos) 
        {
            mContext = context;
            mPos = pos;
        }
        
        @Override
        public void onClick(View v) 
        {
            char[] dayMask = mWeekLoopPolicy.getDayMask();
            if(mPos > dayMask.length)
            {
                return;
            }

            if(dayMask[mPos] == '1')
            {
                dayMask[mPos] = '0';
            }
            else
            {
                dayMask[mPos] = '1';
            }
            mWeekLoopPolicy.setDayMask(dayMask);
            
            mWeekAdapter.notifyDataSetChanged();   
        }
    }

    @Override
    protected void doSetTag(Object object)
    {
        if(object instanceof TLoopPolicyWeek)
        {
            mWeekLoopPolicy = (TLoopPolicyWeek)object;
        }
    }

    @Override
    public void onClose()
    {
        
    }


}
